#region License
// /* **************************************************************
// * Copyright (C) intex Consulting GmbH - All Rights Reserved    *
// *--------------------------------------------------------------*
// * Unauthorized copying of this file, via any medium is         *
// * strictly prohibited                                          *
// *--------------------------------------------------------------*
// * Proprietary and confidential                                 *
// *--------------------------------------------------------------*
// * Dissasembling/modifications strictly prohibited!             *
// ***************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using ComHlp.ContentClasses.FormDef;
using ComHlp.enums;
using ComHlp.enums.Fields;

namespace ComHlp.ContentClasses.VirtualControls
{
    [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
    public sealed class ItxField : ICloneable
    {
        // <summary> fieldnumber </summary>
        public int VirtualFieldNumber { get; set; }

        private ItxProtocolFieldType _mapFieldType;

        public ItxProtocolFieldType MapFieldType
        {
            get { return _mapFieldType; }
            set
            {
                _mapFieldType = value;
                if (_mapFieldType == ItxProtocolFieldType.DataAreaDeactivatedWithoutHelp)
                    IsEnabled = false;
            }
        }

        /// <summary> the field name </summary>
        public string FormDefName { get; set; }

        /// <summary> password box? </summary>
        public bool IsPasswordBox { get; set; }

        /// <summary> contains date? </summary>
        public bool IsDatefield { get; set; }

        /// <summary> the formdef id </summary>
        public string ControlName { get; set; }

        /// <summary> length of the field </summary>
        public int Length { get; set; }

        /// <summary> group where field resides inside </summary>
        public ItxMapGroup Group { get; set; }

        /// <summary> count of constraints </summary>
        public int ConstraintCount { get; set; }

        public ItxFieldFormat Attrib1FieldFormat { get; set; }

        private ItxFieldInternalAttribute _attrib2Internal;

        public ItxFieldInternalAttribute Attrib2Internal
        {
            get { return _attrib2Internal; }
            set
            {
                _attrib2Internal = value;
                if (_attrib2Internal == ItxFieldInternalAttribute.OutputFeldNormalDeactivated
                    || _attrib2Internal == ItxFieldInternalAttribute.OutputFieldBrightDeactivated
                    || _attrib2Internal == ItxFieldInternalAttribute.OutputFieldDarkDeactivated)
                    IsEnabled = false;
            }
        }

        public ItxCharacterCasing Attrib3Casing { get; set; }
        public ItxFieldAlignment Attrib4Alignment { get; set; }

        public ItxFieldType FieldType { get; set; }

        public List<ItxFieldConstraint> FieldConstraints { get; private set; }

        /// <summary> received row number for intex table-layout </summary>
        public int FormDefRowNo { get; set; }

        /// <summary> received cell number for intex table-layout </summary>
        public int FormDefColumnNo { get; set; }

        /// <summary> bs content for the field </summary>
        public string BsContent { get; set; }

        /// <summary> bs attributes for the field </summary>
        public string RecvBsAttribute { get; set; }

        private ItxFieldState _bsAttribute;

        public ItxFieldState BsAttribute
        {
            get { return _bsAttribute; }
            set
            {
                _bsAttribute = value;
                if (BsAttribute == ItxFieldState.Disabled)
                    IsEnabled = false;
            }
        }

        public List<ItxFieldAddition> FieldAdditions { get; private set; }

        public bool BsCursorField { get; set; }

        /// <summary> property to determine if this field is in general enabled
        /// for cursor navigation </summary>
        public bool IsEnabled { get; set; } = true;

        /// <summary> id of listview for vm's </summary>
        public int ListViewNo { get; set; }

        /// <summary> id of assigned listview row for vm's</summary>
        public int ListViewRowNumber { get; set; } = -1;

        /// <summary> converted color no from bs attribute </summary>
        public int BsColorNo { get; set; }

        /// <summary> column index inside listview (not used when vm/vm2!!!11!)</summary>
        public int ListViewColumnIndex { get; set; } = -1;


        /// <summary> Default constructor </summary>
        public ItxField()
        {
            FieldAdditions = new List<ItxFieldAddition>();
            BsAttribute = ItxFieldState.Normal;
            RecvBsAttribute = "";
            BsContent = "";
            FormDefColumnNo = -1;
            FormDefRowNo = -1;
            FieldConstraints = new List<ItxFieldConstraint>();
            FieldType = ItxFieldType.Normal;
            ConstraintCount = -1;
            Length = -1;
            ControlName = "";
            FormDefName = "";
        }

        object ICloneable.Clone()
        {
            return Clone();
        }
        public ItxField Clone()
        {
            ItxField itxField = MemberwiseClone() as ItxField;
            if (itxField == null)
                return null;

            List<ItxFieldAddition> clonedAdditions = new List<ItxFieldAddition>(itxField.FieldAdditions.Count);
            if (itxField.FieldAdditions.Count > 0)
                clonedAdditions.AddRange(itxField.FieldAdditions.Select(fdAdd => fdAdd.Clone()));
            itxField.FieldAdditions = clonedAdditions;

            List<ItxFieldConstraint> clonedConstraints = new List<ItxFieldConstraint>(itxField.FieldConstraints.Count);
            if (itxField.FieldConstraints.Count > 0)
                clonedConstraints.AddRange(itxField.FieldConstraints.Select(fdConst => fdConst.Clone()));

            itxField.FieldConstraints = clonedConstraints;
            return itxField;
        }
    }
}